# -*- coding:utf-8 -*-
from django.shortcuts import render, HttpResponse, redirect, reverse
from django.views.generic.base import View
from django.db.models import Q
from pure_pagination import Paginator, PageNotAnInteger

from .models import MenuType, Menu
from users.models import Advertisement
from operation.models import UserFavorite, UserConcern
from users.models import UserProfile


class CookbookView(View):
    def get(self, request):
        all_menu_type = MenuType.objects.all()
        hot_menu = Menu.objects.all().order_by('-add_time')[:8]
        return render(request, 'cookbook.html', {
            'all_menu_type': all_menu_type,
            'hot_menu': hot_menu,
        })


class CookbookListView(View):
    def get(self, request, menu_type_id):
        all_menu_type = MenuType.objects.all()
        menus = Menu.objects.filter(menu_tpye=int(menu_type_id))
        ad_image = Advertisement.objects.all()
        hot_menus = Menu.objects.all().order_by('-click_number')[:5]
        type = MenuType.objects.get(id=int(menu_type_id))
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(menus, 5, request=request)
        menus = p.page(page)
        return render(request, 'cookbook-classify.html', {
            'all_menu_type': all_menu_type,
            'menus': menus,
            'ad_image': ad_image,
            'hot_menus': hot_menus,
            'type': type,
        })


class CookbookDetailView(View):
    def get(self, request, menu_id):
        all_menu_type = MenuType.objects.all()
        detail = Menu.objects.get(id=int(menu_id))
        detail.click_number += 1
        detail.save()
        ad_image = Advertisement.objects.all()
        hot_menus = Menu.objects.all().order_by('-click_number')[:5]
        menu_detail_type = Menu.objects.get(id=int(menu_id)).menu_tpye
        is_fav = False
        if request.user.is_authenticated():
            user_favorite = UserFavorite.objects.filter(user=request.user, menu=detail)
            if user_favorite and user_favorite[0].is_fav:
                is_fav = True
        if detail.user == request.user:
            is_fav = None
        return render(request, 'cookbook-detail.html', {
            'all_menu_type': all_menu_type,
            'detail': detail,
            'ad_image': ad_image,
            'hot_menus': hot_menus,
            'menu_detail_type': menu_detail_type,
            'is_fav': is_fav,
        })


class AddFavView(View):
    def post(self, request):
        if request.user.is_authenticated():
            fav_status_list = UserFavorite.objects.filter(user=request.user, menu_id=request.POST.get('menu_id'))
            if fav_status_list:
                if fav_status_list[0].is_fav:
                    fav_status_list[0].is_fav = False
                    fav_status_list[0].save()
                    return HttpResponse('{"status": 2}', content_type='application/json')
                else:
                    fav_status_list[0].is_fav = True
                    fav_status_list[0].save()
                    return HttpResponse('{"status": 1}', content_type='application/json')
            else:
                user_fav = UserFavorite()
                user_fav.user = request.user
                user_fav.menu_id = request.POST.get('menu_id')
                user_fav.save()
                return HttpResponse('{"status": 1}', content_type='application/json')
        return HttpResponse('{"status": 0}', content_type='application/json')


class UserMenuView(View):
    def get(self, request, user_id):
        from_user = UserProfile.objects.get(id=int(user_id))
        menus = Menu.objects.filter(user_id=int(user_id))
        ad_image = Advertisement.objects.all()
        hot_menus = Menu.objects.all().order_by('-click_number')[:5]
        is_concern = False
        if request.user.is_authenticated():
            user_concern = UserConcern.objects.filter(user_id=request.user.id, to_user=from_user)
            if user_concern and user_concern[0].is_concern:
                is_concern = True
        if from_user == request.user:
            is_concern = None
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(menus, 5, request=request)
        menus = p.page(page)
        return render(request, 'user-menu.html', {
            'from_user': from_user,
            'menus': menus,
            'ad_image': ad_image,
            'hot_menus': hot_menus,
            'is_concern': is_concern,
        })


class AddConcernView(View):
    def post(self, request):
        if request.user.is_authenticated():
            concern_status_list = UserConcern.objects.filter(user_id=request.user.id, to_user_id=request.POST.get('from_user'))
            if concern_status_list:
                if concern_status_list[0].is_concern:
                    concern_status_list[0].is_concern = False
                    concern_status_list[0].save()
                    return HttpResponse('{"status": 2}', content_type='application/json')
                else:
                    concern_status_list[0].is_concern = True
                    concern_status_list[0].save()
                    return HttpResponse('{"status": 1}', content_type='application/json')
            else:
                user_concern = UserConcern()
                user_concern.user_id = request.user.id
                user_concern.to_user = request.POST.get('from_user')
                user_concern.save()
                return HttpResponse('{"status": 1}', content_type='application/json')
        return HttpResponse('{"status": 0}', content_type='application/json')


class SearchView(View):
    def post(self, request):
        search = request.POST.get('search')
        if search:
            result = Menu.objects.filter(Q(name__contains=search))
            ad_image = Advertisement.objects.all()
            hot_menus = Menu.objects.all().order_by('-click_number')[:5]
            try:
                page = request.GET.get('page', 1)
            except PageNotAnInteger:
                page = 1
            p = Paginator(result, 5, request=request)
            result = p.page(page)
            return render(request, 'search.html', {
                'result': result,
                'ad_image': ad_image,
                'hot_menus': hot_menus,
            })
        return redirect(reverse('index'))
